package com.qianxx.viewlibrary.recyclerview.pull.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * 基础适配器，针对PullRecyclerView设计，尚待完善中
 *
 * @param <T>
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder>
        implements IViewBindData<T>, View.OnClickListener, ItemTouchHelperAdapter {

    private Context context;
    private List<T> list;
    private int layoutId;
    private OnItemClickListener<T> mListener;
    SimpleItemTouchHelperCallback callback;
    OnItemMoveListener onItemMoveListener;

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_NORMAL = 1;
    private View headerView;

    public BaseAdapter(Context context, List<T> list, int layoutId) {
        this.context = context;
        this.list = list;
        this.layoutId = layoutId;
    }

    @Override
    public int getItemViewType(int position) {
        if (headerView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (headerView != null && viewType == TYPE_HEADER)
            return new BaseViewHolder(headerView);
        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);
        view.setOnClickListener(this);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER)
            return;

        if (headerView != null)
            position = position - 1;
        holder.itemView.setTag(position);
        T item = list.get(position);
        onBind(holder, getItemViewType(position), position, item);
    }

//    @Override
//    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
//        super.onAttachedToRecyclerView(recyclerView);
//
//        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
//        if (manager instanceof GridLayoutManager) {
//            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
//            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//                @Override
//                public int getSpanSize(int position) {
//                    return getItemViewType(position) == TYPE_HEADER
//                            ? gridManager.getSpanCount() : 1;
//                }
//            });
//        }
//    }
//
//    @Override
//    public void onViewAttachedToWindow(BaseViewHolder holder) {
//        super.onViewAttachedToWindow(holder);
//        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
//        if (lp != null
//                && lp instanceof StaggeredGridLayoutManager.LayoutParams
//                && holder.getLayoutPosition() == 0) {
//            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
//            p.setFullSpan(true);
//        }
//    }

    /**
     * 获取执行的位置
     *
     * @param holder
     * @return
     */
    public int getRealPosition(BaseViewHolder holder) {
        int position = holder.getLayoutPosition();
        return headerView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return headerView == null ? list.size() : list.size() + 1;
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            int position = (Integer) v.getTag();
            mListener.onItemClick(this, getList().get(position), position);
        }
    }

    public void setHeaderView(View headerView) {
        this.headerView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return headerView;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getLayoutId() {
        return layoutId;
    }

    public void setLayoutId(int layoutId) {
        this.layoutId = layoutId;
    }

    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.mListener = onItemClickListener;
    }

    public interface OnItemClickListener<T> {
        void onItemClick(RecyclerView.Adapter adapter, T t, int position);
    }

    public void setAll(@NonNull List<T> list) {
        this.list.clear();
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    public void addAll(@NonNull List<T> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    public void add(@NonNull T item) {
        add(list.size(), item);
    }

    public void add(int position, @NonNull T item) {
        list.add(position, item);
        this.notifyItemInserted(position);
    }

    public void remove(@NonNull T item) {
        if (list.contains(item)) {
            remove(list.indexOf(item));
        }
    }

    public void remove(int position) {
        if (position >= getItemCount())
            return;
        list.remove(position);
        int internalPosition = headerView == null ? position : position + 1;
        //this.notifyItemRemoved(internalPosition);
        //this.notifyItemRangeChanged(internalPosition, list.size() - internalPosition);
        notifyDataSetChanged();
    }

    private void compatibilityDataSizeChanged(int size) {
        final int dataSize = list == null ? 0 : list.size();
        if (dataSize == size) {
            notifyDataSetChanged();
        }
    }

    public void clear() {
        list.clear();
        this.notifyDataSetChanged();
    }

    /**
     * 移除元素
     *
     * @param position
     */
    @Override
    public void onItemDissmiss(int position) {
        int pos = headerView == null ? position : position - 1;
        if (pos < list.size() && list.size() > 0) {
            if (onItemMoveListener != null)
                onItemMoveListener.onRemove(this, pos, list.get(pos));
            remove(pos);
        }
    }

    /**
     * 元素交换位置
     *
     * @param fromPosition
     * @param toPosition
     */
    @Override
    public void onItemMove(int fromPosition, int toPosition) {
//        int fromPos = headerView == null ? fromPosition : fromPosition - 1;
//        int toPos = headerView == null ? toPosition : toPosition - 1;
//        if (onItemMoveListener != null)
//            onItemMoveListener.onExchange(this, list.get(fromPos), getList().get(toPos));
//        Collections.swap(list, fromPos, toPos);
//        notifyItemMoved(fromPosition, toPosition);
    }

    /**
     * 设置不允许滑动的item
     *
     * @param disallowMoveIndex
     */
    public void setDisallowMoveItem(int disallowMoveIndex) {
        if (callback != null)
            callback.setDisallowMoveItem(disallowMoveIndex);
    }

    /**
     * 设置滑动监听器
     *
     * @param recyclerView
     * @param listener
     */
    public void setOnItemMoveListener(RecyclerView recyclerView, OnItemMoveListener listener) {
        this.onItemMoveListener = listener;
        //绑定划动效果
        callback = new SimpleItemTouchHelperCallback(this);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(recyclerView);
    }
}
